import 'package:shop/common/shop_material.dart';

import 'demo.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  bool _switchSelected = false;
  bool _checkboxSelected = false;
  String str = " ";
  RangeValues _rangeValues = RangeValues(10, 36);

  List<int> list = [0, 1, 2, 5, 5];
  int radioValue = 1;
  // ignore: unused_field
  int _selectedIndex = 0;
  List tabs = ["新闻", "历史", "图片"];
  TabController _tabController; //需要定义一个Controller
  OverlayEntry _overlayEntry;

  double _globlePositionY = 100;
  double _globlePositionX = 100;
  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
    WidgetsBinding.instance.addPostFrameCallback((mag) {
      initOver();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("常用组件"),
        actions: <Widget>[
          //导航栏右侧菜单
          IconButton(icon: Icon(Icons.share), onPressed: () {}),
        ],
        leading: Builder(builder: (context) {
          return IconButton(
            icon: Icon(Icons.dashboard, color: Colors.white), //自定义图标
            onPressed: () {
              Scaffold.of(context).openDrawer();
            },
          );
        }),
        bottom: TabBar(
            //生成Tab菜单
            controller: _tabController,
            tabs: tabs.map((e) => Tab(text: e)).toList()),
      ),
      floatingActionButton: FloatingActionButton(onPressed: () {}),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      drawer: MyDrawer(), //抽屉
      bottomNavigationBar: BottomAppBar(
        color: Colors.white,
        shape: CircularNotchedRectangle(),
        child: Row(
          children: [
            IconButton(
              icon: Icon(Icons.home),
              onPressed: () {},
            ),
            IconButton(
              icon: Icon(Icons.business),
              onPressed: () {},
            ),
          ],
          mainAxisAlignment: MainAxisAlignment.spaceAround, //均分底部导航栏横向空间
        ),
      ),
      body: ListView(
        children: [
          Text(
            "文本",
            style: TextStyle(
              //import 'package:widget/common/shop_material.dart';
              fontSize: 24.s,
              color: Colors.blue,
            ),
          ),

          Text.rich(TextSpan(children: [
            TextSpan(text: "富文本"),
            TextSpan(
              text: "111111",
              style: TextStyle(color: Colors.blue),
            ),
          ])),
          Wrap(
            // 超出会自动换行
            alignment: WrapAlignment.start,
            children: [
              Text("缩放浏览器 , 超出会自动换行:  "),
              RaisedButton(
                child: Text("RaisedButton 按钮"),
                onPressed: () {
                  //点击事件
                },
              ),
              FlatButton(
                child: Text("FlatButton 按钮"),
                onPressed: () {},
              ),
              OutlineButton(
                child: Text("OutlineButton 按钮"),
                onPressed: () {},
              ),
              IconButton(
                // 图标按钮
                icon: Icon(Icons.thumb_up),
                onPressed: () {},
              ),
              RaisedButton.icon(
                icon: Icon(Icons.send),
                label: Text("发送"),
                onPressed: () {},
              ),
              OutlineButton.icon(
                icon: Icon(Icons.add),
                label: Text("添加"),
                onPressed: () {},
              ),
              FlatButton.icon(
                icon: Icon(Icons.info),
                label: Text("详情"),
                onPressed: () {},
              ),
            ],
          ),
          Row(
            children: [
              Text("加载图片:"),
              //  Image.asset("images/avatar.png", width: 100.0) , //加载本地静态图片
              GestureDetector(
                onTap: () {
                  DigetDemo.showSimpleDialog(
                      context,
                      Hero(
                        tag: "123456",
                        child: Image(
                          image: NetworkImage(
                              "https://pcdn.flutterchina.club/imgs/3-17.png"),
                          fit: BoxFit.fitWidth,
                          height: MediaQuery.of(context).size.height,
                          width: MediaQuery.of(context).size.width,
                        ),
                      ));
                },
                child: Hero(
                  tag: "123456",
                  child: Image(
                      image: NetworkImage(
                          "https://pcdn.flutterchina.club/imgs/3-17.png")),
                ),
              )
            ],
          ),
          Row(
            children: [
              Text("图标:"),
              Icon(Icons.send),
              Icon(Icons.edit, color: Colors.green),
              Icon(Icons.email, color: Colors.red),
            ],
          ),
          Row(
            children: [
              Text("Switch:"),
              Switch(
                value: _switchSelected, //当前状态
                onChanged: (value) {
                  //重新构建页面
                  setState(() {
                    _switchSelected = value;
                  });
                },
              ),
              Text("Checkbox:"),
              Checkbox(
                value: _checkboxSelected,
                activeColor: Colors.red, //选中时的颜色
                onChanged: (value) {
                  setState(() {
                    _checkboxSelected = value;
                  });
                },
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: Radio(
                    value: 1,
                    groupValue: radioValue,
                    onChanged: (value) {
                      setState(() {
                        radioValue = value;
                      });
                    }),
              ),
              Expanded(
                child: RadioListTile(
                    title: Text("第二个单选按钮"),
                    value: 2,
                    groupValue: radioValue,
                    onChanged: (value) {
                      setState(() {
                        radioValue = value;
                      });
                    }),
              ),
              Expanded(
                  child: RadioListTile(
                      title: Text("第三个单选按钮"),
                      value: 3,
                      groupValue: radioValue,
                      onChanged: (value) {
                        setState(() {
                          radioValue = value;
                        });
                      }))
            ],
          ),
          TextField(
            keyboardType: TextInputType.emailAddress,
            decoration: InputDecoration(
                labelText: "输入框",
                hintText: "这是一个输入框",
                prefixIcon: Icon(Icons.email),
                border: InputBorder.none //隐藏下划线
                ),
            maxLength: 15,
          ),
          //进度条显示50%
          Row(
            children: [
              Text("进度条"),
              Expanded(
                child: LinearProgressIndicator(
                  valueColor: AlwaysStoppedAnimation(Colors.blue),
                  value: 0.5,
                ),
              ),
              Expanded(
                child: LinearProgressIndicator(
                  backgroundColor: Colors.grey[200],
                  valueColor: AlwaysStoppedAnimation(Colors.blue),
                ),
              ),
              Expanded(
                child: AspectRatio(
                  aspectRatio: 1,
                  child: CircularProgressIndicator(
                    backgroundColor: Colors.grey[200],
                    valueColor: AlwaysStoppedAnimation(Colors.blue),
                    value: 0.5,
                  ),
                ),
              ),
              Expanded(
                child: AspectRatio(
                  aspectRatio: 1,
                  child: CircularProgressIndicator(
                    backgroundColor: Colors.grey[200],
                    valueColor: AlwaysStoppedAnimation(Colors.blue),
                  ),
                ),
              ),
            ],
          ),
          Row(
            children: [
              Text("这是一行"),
              Text("这是一行"),
              Text("这是一行"),
              Text("这是一行"),
              Text("这是一行"),
            ],
          ),
          Column(
            children: [
              Text("这是一列"),
              Text("这是一列"),
              Text("这是一列"),
              Text("这是一列"),
            ],
          ),
          Chip(
            avatar: new CircleAvatar(
                backgroundColor: Colors.blue, child: Text('组件')),
            label: new Text("圆角"),
            deleteIcon: Icon(Icons.delete),
          ),

          Row(
            children: [
              Text("绝对布局:  "),
              Container(
                height: 200,
                width: 200,
                color: Colors.red,
                child: Stack(
                  children: [
                    Text("左上角"),
                    Align(
                      alignment: Alignment.bottomRight,
                      child: Text("右下角"),
                    ),
                    Align(
                      alignment: Alignment.center,
                      child: Text("居中"),
                    ),
                    Positioned(bottom: 10, child: Text("距离底部10px"))
                  ],
                ),
              )
            ],
          ),
          Row(
            children: [
              Container(
                padding: EdgeInsets.all(10),
                margin: EdgeInsets.all(20),
                color: Colors.orange,
                child: Container(
                  height: 100,
                  width: 100,
                  color: Colors.green,
                  alignment: Alignment.center,
                  child: Text("内边距10,外边距20 , 子组件居中"),
                ),
              )
            ],
          ),
          Row(
            children: [
              ClipOval(
                child: Container(
                  height: 100,
                  width: 100,
                  color: Colors.red,
                  child: Text("裁剪成圆形"),
                ),
              ),
              ClipRRect(
                child: Container(
                  height: 100,
                  width: 100,
                  color: Colors.red,
                  child: Text("圆角矩形"),
                ),
              ),
            ],
          ),
          Container(
            height: 200,
            color: Colors.blue[50],
            child: ListView(
              children: List.generate(100, (index) => Text("listVie$index")),
            ),
          ),
          Divider(
            height: 20,
            color: Colors.red,
          ),

          Container(
            height: 200,
            color: Colors.greenAccent,
            child: GridView(
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3, //横轴三个子widget
                ),
                children: List.generate(50, (index) => Text("网格布局$index"))),
          ),
          FlatButton(
              onPressed: () {
                DigetDemo.showAlertDialog(context);
              },
              child: Text('showAlertDialog')),

          FlatButton(
              onPressed: () {
                DigetDemo.showModalBottom(
                  context,
                  Container(
                    child: Stack(
                      children: [
                        Positioned(
                            top: 30,
                            width: MediaQuery.of(context).size.width,
                            child: Container(
                              height: 170,
                              decoration: BoxDecoration(
                                  color: Colors.red,
                                  borderRadius: BorderRadius.circular(20)),
                              child: Text('data'),
                            )),
                        Positioned(
                          height: 50,
                          left: 50,
                          child: AspectRatio(
                            aspectRatio: 1,
                            child: Image.network(
                              "https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=204471953,2683458197&fm=26&gp=0.jpg",
                              height: 30,
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                );
              },
              child: Text('showModalBottom')),
          FlatButton(
              onPressed: () {
                //DigetDemo.showSimpleDialog(context);
              },
              child: Text('showSimpleDialog')),
          ListView.builder(
            itemCount: list.length,
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (BuildContext context, int index) {
              return Row(
                children: [
                  FlatButton(
                      onPressed: () {
                        setState(() {
                          list[index] -= 1;
                        });
                      },
                      child: Text("-")),
                  Text("${list[index]}"),
                  FlatButton(
                      onPressed: () {
                        setState(() {
                          list[index] += 1;
                        });
                      },
                      child: Text("+")),
                ],
              );
            },
          ),

          ListTile(
            title: Text("一级标题"),
            subtitle: Text("二级标题"),
            leading: Container(
              height: 200,
              color: Colors.red,
              child: Text("放个图片"),
            ),
            trailing: Checkbox(value: true, onChanged: (v) {}),
          ),
          RangeSlider(
            values: _rangeValues,
            min: 0,
            max: 100.0,
            onChanged: (RangeValues newValues) {
              setState(() {
                _rangeValues = newValues;
              });
            },
          ),
          Row(
            children: [
              Text("圆形头像: "),
              CircleAvatar(
                backgroundImage: NetworkImage(
                    'https://images.gitee.com/uploads/91/465191_vsdeveloper.png'),
              )
            ],
          ),
          Text(
            String.fromCharCode(128513),
            style: TextStyle(fontSize: 33),
          ),

          InkWell(
            onTap: () {}, //点击事件
            child: Text("子组件"),
          ),
        ],
      ),
    );
  }

  void initOver() {
    _overlayEntry = OverlayEntry(builder: (context1) {
      return StatefulBuilder(
        builder: (BuildContext context, setState) {
          return Stack(
            children: <Widget>[
              Positioned(
                top: _globlePositionY,
                left: _globlePositionX,
                child: GestureDetector(
                  child: Material(
                    child: Container(
                      height: 100,
                      width: 100,
                      color: Colors.red,
                      child: Center(
                        child: Text("长按可拖动"),
                      ),
                    ),
                  ),
                  onLongPressMoveUpdate: (LongPressMoveUpdateDetails details) {
                    setState(() {
                      _globlePositionX = details.globalPosition.dx - 50;
                      _globlePositionY = details.globalPosition.dy - 50;
                      if (_globlePositionX + 100 >
                          MediaQuery.of(context).size.width) {
                        _globlePositionX =
                            MediaQuery.of(context).size.width - 100;
                      }
                      if (_globlePositionY + 100 >
                          MediaQuery.of(context).size.height) {
                        _globlePositionY =
                            MediaQuery.of(context).size.height - 100;
                      }
                      if (_globlePositionX < 0) _globlePositionX = 0;
                      if (_globlePositionY < 0) _globlePositionY = 0;
                    });
                  },
                ),
              )
            ],
          );
        },
      );
    });
    Overlay.of(context).insert(_overlayEntry);
  }
}

class MyDrawer extends StatelessWidget {
  const MyDrawer({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: MediaQuery.removePadding(
        context: context,
        //移除抽屉菜单顶部默认留白
        removeTop: true,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(top: 38.0),
              child: Row(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    child: ClipOval(
                      child: Text("data"),
                    ),
                  ),
                  Text(
                    "ttttt",
                    style: TextStyle(fontWeight: FontWeight.bold),
                  )
                ],
              ),
            ),
            Expanded(
              child: ListView(
                children: <Widget>[
                  ListTile(
                    leading: const Icon(Icons.add),
                    title: const Text('Add account'),
                  ),
                  ListTile(
                    leading: const Icon(Icons.settings),
                    title: const Text('Manage accounts'),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
